package com.example.bmanliku;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{
        private EditText account,passwords,telnumber;
        private Button Bt_submit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    private void initView() {
        account=findViewById(R.id.account);
        passwords=findViewById(R.id.passwords);
        telnumber=findViewById(R.id.telnumber);
        Bt_submit=findViewById(R.id.Bt_submit);
        Bt_submit.setOnClickListener(this);
    }

    @Override
    public void onClick(@org.jetbrains.annotations.NotNull View v)
    {
        switch (v.getId())
        {
            case R.id.Bt_submit:
                submit();
                break;
        }
    }


    private void submit() {
        // validate
        String accountString = account.getText().toString().trim();
        if (TextUtils.isEmpty(accountString)) {
            Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
            return;
        }

        String PasswordString = passwords.getText().toString().trim();
        if (TextUtils.isEmpty(PasswordString)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }

        String telnumberString = telnumber.getText().toString().trim();
        if (TextUtils.isEmpty(telnumberString)) {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        //  validate success, do something
        InternetSever(accountString,PasswordString,telnumberString);
    }
    private void InternetSever(String username,String password,String telnumber) {
        OkHttpClient client = new OkHttpClient();
        FormBody body = new FormBody.Builder()
                .add("username", username)
                .add("password", password)
                .add("telnumber", telnumber)
                .build();
        final Request request = new Request.Builder()
                .url("http://192.168.1.7:8000/registe/")
                .post(body)
                .build();
        client.newCall(request).enqueue(new Callback()
        {

            @Override
            public void onFailure(Call call, IOException e) {
                Toast.makeText(RegisterActivity.this, "网络异常，请稍后再试！", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException
            {

                final String date=response.body().string();
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Gson gson=new Gson();
                        Register_cs register_cs=gson.fromJson(date,Register_cs.class);
                        if (register_cs.get_$StatusCode185()==200){
                            Toast.makeText(RegisterActivity.this, "欢迎登录，", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(RegisterActivity.this,IndexActivity.class));
                        }else {
                            Toast.makeText(RegisterActivity.this, Register_cs.getCode(), Toast.LENGTH_SHORT).show();
                            account.setText("");
                            passwords.setText("");
                        }
                    }
                });

            }
        });


    }


}